do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5
	
gen tot_asset = pers_asset + spouse_asset
gen tot_rlest = pers_rlest + spouse_rlest

foreach var in rlest{
	gen v1 = pers_`var' if norm == -1
	egen pers_`var'_pre1 = mean(v1), by(houseid)
	drop v1
	gen v1 = tot_`var' if norm == -1
	egen tot_`var'_pre1 = mean(v1), by(houseid)
	drop v1
}

foreach var in saving liquidasset rlest{
	gen positive_`var'_pre1 = pers_`var'_pre1 > 0
}
gen positive_totrlest_pre1 = tot_rlest_pre1 > 0


replace norm_year = norm_year + 3	

foreach grp in saving liquidasset rlest{
	foreach x in currentxprizexpre3 currentxprizexpre2 currentxprizexpost0 currentxprizexpost1 currentxprizexpost2 currentxprizexpost3 currentxprizexpost4 currentxprizexpost5 currentxprizexpost6{
		gen `grp'x`x' = 0
		replace `grp'x`x' = `x' if positive_`grp'_pre1 == 1
	}	
}

cap rm "$table/temp/Tab4_Interaction.txt"
cap rm "$table/temp/Tab4_Interaction.xls"

foreach y in totbaby {
	foreach grp in saving liquidasset rlest{
		
		global control1 "i.pers_gender#i.positive_`grp'_pre1 i.houhld_resid_cd#i.positive_`grp'_pre1 c.pers_income_pre1#i.positive_`grp'_pre1 c.pers_asset_pre1#i.positive_`grp'_pre1 c.pers_earnings_pre1#i.positive_`grp'_pre1 i.pers_labor_pre1#i.positive_`grp'_pre1 i.married_pre1#i.positive_`grp'_pre1"
		global control2 "c.totbaby_pre1#i.positive_`grp'_pre1"
		global control3 "c.tot_redeem_lotto#i.positive_`grp'_pre1 c.tot_redeem_receipt#i.positive_`grp'_pre1"

		reghdfe `y' currentxprizexp* `grp'xcurrentxprizexp* c.prize10M#i.positive_`grp'_pre1 c.prize10M#i.norm#i.positive_`grp'_pre1 c.prize10M#i.current#i.positive_`grp'_pre1 $control1 $control2 $control3, a(norm#current#positive_`grp'_pre1 year#positive_`grp'_pre1 age#positive_`grp'_pre1) cl(houseid)
			
		outreg2 using "$table/temp/Tab4_Interaction.xls" , append bd(3) sd(3) nocon keep(*currentxprize*) ctitle(`grp')
	}
}

import delimited using "$table/temp/Tab4_Interaction.txt", clear

replace v1 = substr(v1,strpos(v1,"current"),.) if strpos(v1,"current") != 0
replace v1 = "SubGrp#" + v1 if inrange(_n,22,111) & v1 != ""

forv i = 3(1)6{
	forv j = 1(1)18{
		loc k = `j'+21
		loc m = (`i'-2)*18
			replace v`i' = v`i'[_n+`m'] in `k'
	}
}

drop in 40/111

replace v2 = "Deposit" in 2
replace v3 = "Liquid Asset" in 2
replace v4 = "Real Estate" in 2

forv i = 1(1)4{
	rename v`i' vv`i'
}

drop in 4/21


save "$table/temp/Tab4-I.dta", replace

export excel "$table/Tables.xlsx", sheet("Tab4-I") sheetreplace
